<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DataSchema Tests</title>
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
<script type="text/javascript" src="../../../build/dataschema/dataschema-debug.js"></script>
</head>

<body class="yui-skin-sam">
<h1>DataSchema Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>

<script type="text/javascript">

(function() {
    YUI({
        base: "../../../build/",
        //filter: "debug",
        logInclude:{"TestRunner":true},
        useConsole: true
    }).use("console", "test", "dump", "dataschema", "datatype", function(Y) {
        
        // Set up the page
        var ASSERT = Y.Assert,
            ARRAYASSERT = Y.ArrayAssert,
            OBJECTASSERT = Y.ObjectAssert,
            btnRun = Y.get("#btnRun"),
            myConsole = new Y.Console().render();
            
        btnRun.set("disabled", false);
        Y.on("click", function(){
            Y.Test.Runner.run();
        }, btnRun);


        var testJSON = new Y.Test.Case({
            name: "JSON Tests",
        
            testJSONMeta: function() {
                var schema = {
                        metaFields: {top:"top", nested:"second.nested"}
                    },
                    data_in = {
                        top: "foo",
                        second: {nested: "bar"}
                    },
                    data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                ASSERT.areSame("foo", data_out.meta.top, "Expected first meta.");
                ASSERT.areSame("bar", data_out.meta.nested, "Expected second meta.");
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(0, data_out.results.length, "Expected zero results.");
            },

            testJSONResults: function() {
                var schema = {
                        resultListLocator: "response",
                        resultFields: ["name", {key:"number", parser:Y.DataType.Number.parse}, "letter"]
                    },
                    data_in = {
                        response: [
                            {name: "alpha", number: "0", letter: "a"},
                            {name: "beta", number: "1", letter: "b"},
                            {name: "gamma", number: "2", letter: "c"}
                        ]
                    },
                    data_out = Y.DataSchema.JSON.apply(schema, data_in);
                    
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("alpha", data_out.results[0].name, "Expected first result.");
                ASSERT.areSame("gamma", data_out.results[2].name, "Expected last result.");
                ASSERT.areSame(0, data_out.results[0].number, "Expected first number to be parsed.");
                ASSERT.areSame(2, data_out.results[2].number, "Expected last number to be parsed.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },
            
            testJSONComplexLocators: function() {
                var schema = {
                        metaFields: {metaTotal:"response['meta-total']", metaMessage:"response['data array'][1].response.message"},
                        resultListLocator:"response['data array'][0].response['i t e m s']",
                        resultFields: [
                            "name",
                            "['customer.data']['alert(\'id\')']",
                            "['customer.data']['phone/email']",
                            "['array-pets'][0].name",
                            "['array-pets']"
                        ]
                    },
                    data_in = {
request: "orig request", response: { "meta-total": 3, "data array" : [{ position: "array.item.0", "response": { "i t e m s": [ { /* result 0 */ name: "Bob", "customer.data": { "alert('id')":10, "phone/email": "123-4567", "catch":true }, "array-pets": [ { /* pet 0 */ "name":"Brutus", "breed": "bulldog" }, { /*  pet 1 */ "name":"Bobo", "breed": "bernese" } ] }, { /* result 1 */ name: "Fran", "customer.data": { "alert('id')":11, "phone/email": "234-5678", "catch":false}, "array-pets": [ { /*  pet 0 */ "name":"Fido", "breed": "french poodle" } ] } ] } }, { position: "array.item.1", "response": { "message": { type: "xyz", error: false } } }], metaid: { id:1234, date: new Date() } }                    },
                    data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                ASSERT.areSame(3, data_out.meta.metaTotal, "Expected meta total.");
                ASSERT.areSame("xyz", data_out.meta.metaMessage.type, "Expected nested meta message type.");
                ASSERT.areSame(false, data_out.meta.metaMessage.error, "Expected nested meta message error.");
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(2, data_out.results.length, "Expected 2 results.");
                ASSERT.areSame(10, data_out.results[0]["['customer.data']['alert(\'id\')']"], "Expected first id.");
                ASSERT.areSame("234-5678", data_out.results[1]["['customer.data']['phone/email']"], "Expected second phone/email.");
                ASSERT.areSame("Fido", data_out.results[1]["['array-pets'][0].name"], "Expected nested array item data.");
                ASSERT.isArray(data_out.results[0]["['array-pets']"], "Expected array.");
                ASSERT.areSame(2, data_out.results[0]["['array-pets']"].length, "Expected array length.");
            },
            
            testJSONNested: function() {
                var schema = {resultListLocator: "reference[1].fruit",
                    resultFields: ["name","calories"]
                },
                data_in = {
                    "profile":{
                        "current":160,
                        "target":150
                    },
                    "reference": [
                        {
                            "category":"exercise",
                            "type":"expenditure",
                            "activities":[
                                {"name":"biking", "calories":550},
                                {"name":"golf", "calories":1000},
                                {"name":"running", "calories":650},
                                {"name":"swimming", "calories":650},
                                {"name":"walking", "calories":225}
                            ]
                        },
                        {
                            "category":"nutrition",
                            "type":"intake",
                            "fruit":[
                                {"name":"apple", "calories":70},
                                {"name":"banana", "calories":70},
                                {"name":"orange", "calories":90},
                            ],
                            "vegetables":[
                                {"name":"baked potato", "calories":150},
                                {"name":"broccoli", "calories":50},
                                {"name":"green beans", "calories":30}
                            ]
                        }
                    ],
                    "program": [
                        {
                            "category":"exercise",
                            "schedule":[
                                {"day":"sunday", "activity":"swimming"},
                                {"day":"monday", "activity":"running"},
                                {"day":"tuesday", "activity":"biking"},
                                {"day":"wednesday", "activity":"running"},
                                {"day":"thursday", "activity":"swimming"},
                                {"day":"friday", "activity":"running"},
                                {"day":"saturday", "activity":"golf"}
                            ]
                        },
                        {
                            "category":"diet",
                            "schedule":[
                            ]
                        }
                    ]
                },
                data_out = Y.DataSchema.JSON.apply(schema, data_in);
                
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
            },
    
            testJSONError: function() {
                var schema = {
                        resultListLocator: "response",
                        resultFields: ["name", "number", "letter"]
                    },
                    data_in = 123,
                    data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                //OBJECTASSERT.isEmpty(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected JSON parse error.");

                data_in = {123:123, 456:456};
                data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                //OBJECTASSERT.isEmpty(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected results error.");

                data_in = {"response":"abc"};
                data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                //OBJECTASSERT.isEmpty(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected fields error.");

                data_in = {"query":[
                    {name: "alpha", number: "0", letter: "a"},
                    {name: "beta", number: "1", letter: "b"},
                    {name: "gamma", number: "2", letter: "c"}
                ]};
                schema.resultListLocator = "ResultSet.Result";
                
                data_out = Y.DataSchema.JSON.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                //OBJECTASSERT.isEmpty(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected results error.");
            }
        });
            
        var testXML = new Y.Test.Case({
            name: "XML Tests",

            testXMLMeta: function() {
                var schema = {
                        metaFields: {rootatt:{parser:Y.DataType.Number.parse, locator:"/myroot/@rootatt"}, topnode:"//top", nestedatt:"//second/@nested"}
                    },
                    data_in = Y.DataType.XML.parse("<myroot rootatt='0'><top>topvalue</top><second nested='nestedvalue' /><item type='foo'><name>Abc</name><rank>0</rank></item><item type='bar'><name>Def</name><rank>1</rank></item><item type='bat'><name>Ghi</name><rank>2</rank></item></myroot>"),
                    data_out = Y.DataSchema.XML.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                ASSERT.areSame(0, data_out.meta.rootatt, "Expected rootatt.");
                ASSERT.areSame("topvalue", data_out.meta.topnode, "Expected topvalue.");
                ASSERT.areSame("nestedvalue", data_out.meta.nestedatt, "Expected nestedvalue.");
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(0, data_out.results.length, "Expected zero results.");
            },

            testXMLResults: function() {
                var schema = {
                        resultListLocator: "item",
                        resultFields: [{key:"type", locator:"@type"}, {key:"rank", parser:Y.DataType.Number.parse}, "name"]
                    },
                    data_in = Y.DataType.XML.parse("<myroot><item type='foo'><name>Abc</name><rank>0</rank></item><item type='bar'><name>Def</name><rank>1</rank></item><item type='bat'><name>Ghi</name><rank>2</rank></item></myroot>"),
                    data_out = Y.DataSchema.XML.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("foo", data_out.results[0].type, "Expected first result.");
                ASSERT.areSame("bat", data_out.results[2].type, "Expected last result.");
                ASSERT.areSame(0, data_out.results[0].rank, "Expected first number to be parsed.");
                ASSERT.areSame(2, data_out.results[2].rank, "Expected last number to be parsed.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testXMLComplex: function() {
                var schema = {
                        metaFields: {rootatt:{parser:Y.DataType.Number.parse, locator:"/myroot/@rootatt"}, topnode:"//top", nestedatt:"//second/@nested"},
                        resultListLocator: "item",
                        resultFields: [{key:"type", locator:"@type"}, {key:"rank", parser:Y.DataType.Number.parse}, "name", {key:"subnameatt", locator:"subitem/name/@type"}, {key:"age", locator:"subitem/age", parser:"number"}]
                    },
                    data_in = Y.DataType.XML.parse("<myroot rootatt='5'><top>topvalue</top><second nested='nestedsecond' /><allitems><livehere><item type='foo'><name type='nametype0'>Abc</name><rank>0</rank><subitem><name type='subnametype0'>subABC</name><age>10</age></subitem></item><item type='bar'><name type='nametype1'>Def</name><rank>1</rank><subitem><name type='subnametype1'>subDEF</name><age>11</age></subitem></item><item type='bat'><name type='nametype2'>Ghi</name><rank>2</rank><subitem><name type='subnametype2'>subGHI</name><age>12</age></subitem></item></livehere></allitems></myroot>"),
                    data_out = Y.DataSchema.XML.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                ASSERT.areSame(5, data_out.meta.rootatt, "Expected rootatt.");
                ASSERT.areSame("topvalue", data_out.meta.topnode, "Expected topvalue.");
                ASSERT.areSame("nestedsecond", data_out.meta.nestedatt, "Expected nestedvalue.");
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("foo", data_out.results[0].type, "Expected first type att.");
                ASSERT.areSame("bat", data_out.results[2].type, "Expected last type att.");
                ASSERT.areSame(0, data_out.results[0].rank, "Expected first number to be parsed.");
                ASSERT.areSame(2, data_out.results[2].rank, "Expected last number to be parsed.");
                ASSERT.areSame("subnametype0", data_out.results[0].subnameatt, "Expected first subitem name attribute.");
                ASSERT.areSame("subnametype2", data_out.results[2].subnameatt, "Expected last subitemnameattribute.");
                ASSERT.areSame(10, data_out.results[0].age, "Expected first age.");
                ASSERT.areSame(12, data_out.results[2].age, "Expected last age.");
            },

            testXMLError: function() {
                var schema = {
                        resultListLocator: "none",
                        resultFields: ["type", {key:"rank", parser:Y.DataType.Number.parse}, "name"]
                    },
                   data_in = Y.DataType.XML.parse("<root><item type='foo'><name>Abc</name><rank>1</rank></item><item type='bar'><name>Def</name><rank>2</rank></item></root>"),
                   data_out = Y.DataSchema.XML.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                OBJECTASSERT.ownsNoKeys(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected XML parse error.");
                
                data_out = null;
                data_in = [1,2,3];
                schema.resultsListLocator = "item";
                data_out = Y.DataSchema.XML.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                OBJECTASSERT.ownsNoKeys(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected XML parse error.");
            }
        });

        var testArray = new Y.Test.Case({
            name: "Array Tests",

            testSimpleArrayResults: function() {
                var schema = {
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = ["a","b","c"],
                    data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("a", data_out.results[0], "Expected first string.");
                ASSERT.areSame("c", data_out.results[2], "Expected last string.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testNestedArrayResults: function() {
                var schema = {
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = [["a","0","c"], ["d","1","f"], ["g","-2","i"]],
                    data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("a", data_out.results[0].type, "Expected first type.");
                ASSERT.areSame("g", data_out.results[2].type, "Expected last type.");
                ASSERT.areSame(0, data_out.results[0].age, "Expected first age.");
                ASSERT.areSame(-2, data_out.results[2].age, "Expected last age.");
                ASSERT.areSame("c", data_out.results[0].name, "Expected first name.");
                ASSERT.areSame("i", data_out.results[2].name, "Expected last name.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testObjectArrayResults: function() {
                var schema = {
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}],
                    data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("a", data_out.results[0].type, "Expected first type.");
                ASSERT.areSame("g", data_out.results[2].type, "Expected last type.");
                ASSERT.areSame(0, data_out.results[0].age, "Expected first age.");
                ASSERT.areSame(-1, data_out.results[2].age, "Expected last age.");
                ASSERT.areSame("c", data_out.results[0].name, "Expected first name.");
                ASSERT.areSame("i", data_out.results[2].name, "Expected last name.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testComboArrayResults: function() {
                var schema = {
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = [["a","0","c"], "abc", {type:"g",age:-1,name:"i"}],
                    data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("a", data_out.results[0].type, "Expected first type.");
                ASSERT.areSame("g", data_out.results[2].type, "Expected last type.");
                ASSERT.areSame(0, data_out.results[0].age, "Expected first age.");
                ASSERT.areSame(-1, data_out.results[2].age, "Expected last age.");
                ASSERT.areSame("c", data_out.results[0].name, "Expected first name.");
                ASSERT.areSame("i", data_out.results[2].name, "Expected last name.");
                ASSERT.areSame("abc", data_out.results[1], "Expected second string.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testArrayError: function() {
                var schema = {
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = "abc",
                    data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                OBJECTASSERT.ownsNoKeys(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected array parse error.");

                data_in = [["a","0","c"], function() {}, {type:"g",age:-1,name:"h"}];
                data_out = Y.DataSchema.Array.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(null, data_out.results[1], "Expected null result.");
            }
        });

        var testText = new Y.Test.Case({
            name: "Text Tests",

            testTextResultsAndFields: function() {
                var schema = {
                        resultDelimiter: "\n",
                        fieldDelimiter: "\t",
                        resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
                    },
                    data_in = "foo\t0\tabc\nbar\t1\tdef\nbat\t-1\tghi",
                    data_out = Y.DataSchema.Text.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("foo", data_out.results[0].type, "Expected first type.");
                ASSERT.areSame("bat", data_out.results[2].type, "Expected last type.");
                ASSERT.areSame(0, data_out.results[0].age, "Expected first age.");
                ASSERT.areSame(-1, data_out.results[2].age, "Expected last age.");
                ASSERT.areSame("abc", data_out.results[0].name, "Expected first name.");
                ASSERT.areSame("ghi", data_out.results[2].name, "Expected last name.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },
            
            testTextResultsOnly: function() {
                var schema = {
                        resultDelimiter: ","
                    },
                    data_in = "foo,bar,bat,",
                    data_out = Y.DataSchema.Text.apply(schema, data_in);

                ASSERT.isArray(data_out.results, "Expected results property.");
                ASSERT.areSame(3, data_out.results.length, "Expected 3 results.");
                ASSERT.areSame("foo", data_out.results[0], "Expected first result.");
                ASSERT.areSame("bat", data_out.results[2], "Expected last result.");
                ASSERT.isObject(data_out.meta, "Expected meta property.");
            },

            testTextError: function() {
                var schema = {
                        resultDelimiter: "\n",
                        fieldDelimiter: ","
                    },
                    data_in = 123,
                    data_out = Y.DataSchema.Text.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                OBJECTASSERT.ownsNoKeys(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected text parse error.");
                
                schema = {
                    foo: "bar"
                };
                data_in = "foo,0,abc\nbar,1,def\nbat,-1,ghi",
                data_out = Y.DataSchema.Text.apply(schema, data_in);

                ASSERT.isObject(data_out.meta, "Expected meta property.");
                OBJECTASSERT.ownsNoKeys(data_out.meta, "Expected empty meta.");
                ARRAYASSERT.isEmpty(data_out.results, "Expected empty results.");
                ASSERT.isObject(data_out.error, "Expected text parse error.");
            }
        });

        Y.Test.Runner.add(testJSON);
        Y.Test.Runner.add(testXML);
        Y.Test.Runner.add(testArray);
        Y.Test.Runner.add(testText);
        Y.Test.Runner.run();
    });
})();
</script>
</body>
</html>
